<?php
/* @var $this NewsController */
/* @var $model News */
/* @var $form CActiveForm */
?>
<div class="row-fluid">
    <div class="span12 box">
        <div class="box-header blue-background">
            <div class="title">
                <div class="icon-edit"></div>
                Form News
            </div>
            <div class="actions">
<!--                <a href="#" class="btn box-remove btn-mini btn-link"><i class="icon-remove"></i>
                </a>-->
                <a href="#" class="btn box-collapse btn-mini btn-link"><i></i>
                </a>
            </div>
        </div>
        <div class="box-content">
            <?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'news-form',
                // Please note: When you enable ajax validation, make sure the corresponding
                // controller action is handling ajax validation correctly.
                // There is a call to performAjaxValidation() commented in generated controller code.
                // See class documentation of CActiveForm for details on this.
                'enableAjaxValidation' => false,
                'htmlOptions' => array(
                    'enctype' => 'multipart/form-data',
                    'class' => 'form form-horizontal',),
            ));
            ?>
            <?php echo $form->errorSummary($model); ?>
            <div class="control-group">                    
                <?php echo $form->labelEx($model, 'name', array('class' => 'control-label')); ?>
                <div class="controls">
                    <?php echo $form->textField($model, 'name', array('size' => 60, 'maxlength' => 255)); ?>
                    <?php echo $form->error($model, 'name'); ?>
                </div>
            </div>
            <div class="control-group">
                <?php echo $form->labelEx($model, 'description', array('class' => 'control-label')); ?>
                <div class="controls">
                    <?php echo $form->textArea($model, 'description', array('size' => 60, 'maxlength' => 500)); ?>
                    <?php echo $form->error($model, 'description'); ?>
                </div>
            </div>

            <hr class="hr-normal">
            <div class="control-group">
                <?php echo $form->labelEx($model, 'content', array('class' => 'control-label')); ?>
                <div class="controls">

                    <?php echo $form->textArea($model, 'content', array('id' => 'news_content', 'size' => 60, 'maxlength' => 5000)); ?>
                    <?php echo $form->error($model, 'content'); ?>


                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="Image">Image</label>
                <div class="controls">
                    <div class="span4">
                        <input id="file0" name="images[0]" type='file' onchange="readURL(this, '0');" />
                        <div id="showimg0" style="width: 200px; height: 40px; display: block;"></div>
                    </div>
                    <div class="span4">
                        <input id="file1" name="images[1]" type='file' onchange="readURL(this, '1');" />
                        <div id="showimg1" style="width: 200px; height: 40px; display: block;"></div>
                    </div>
                    <div class="span4">
                        <input id="file2" name="images[2]" type='file' onchange="readURL(this, '2');" />
                        <div id="showimg2" style="width: 200px; height: 40px; display: block;"></div>
                    </div>

                    <div class="photos span12">
                        <?php if (!($model->isNewRecord)) : ?>
                            <?php 
                            foreach ($photos as $index => $photo) :
                            	$attrs = $photo->getAttributes();
                            	echo '<div class="span4">';
                                echo '<img id="image'.$index.'"  src="'.Yii::app()->request->baseUrl.$attrs['path'].'" width="80px" style="margin-top:-45px;"/>';
                                echo '<input value="'.$index.'" type="hidden" name="oldimages['.$index.']" />';
                                echo '<input value="'.$attrs['id'].'" type="hidden" name="images'.$index.'" />';
                                echo '</div>';
                            endforeach;
                            ?>
                        <?php endif; ?>

                    </div>

                </div>
            </div>

            <div class="form-actions">
                <button class="btn btn-primary" type="submit">

                    <i class="icon-save"></i>
                    <?php echo $model->isNewRecord ? 'Create' : 'Save' ?>
                </button>
                <a href="<?php echo $this->createUrl('/news/admin'); ?>">
                    <button class="btn" type="button">Cancel</button>
                </a>
            </div>

            <?php $this->endWidget(); ?>
        </div>
    </div>
</div>

<script type="text/javascript">

function readURL(me, id) {
	$('.file-input-name').html('');
    if (me.files && me.files[0]) {
        var reader = new FileReader();

        reader.onload = function(e) {

			//var checkImage = $('#file' + id).length;
			var checkImage = $('.photos').find($('#image' + id)).length;
            if (checkImage) {
            	$('#image' + id).attr('src', e.target.result).width(80).css('margin-top', '-45px');
            } else {
            	var htmlPattern = '<img id="image' + id + '" width="80px" src="' + e.target.result + '" />';
                $('#showimg' + id).html(htmlPattern);
            }
        };

        reader.readAsDataURL(me.files[0]);
    }
}

CKEDITOR.replace('news_content',{
	toolbar : 'basic',
	enterMode : CKEDITOR.ENTER_BR,
	shiftEnterMode: CKEDITOR.ENTER_P
	//fullPage: true, //=>html|head|body tags. Using FCKConfig.ProtectedTags = 'html|head|body';
	//allowedContent: true
	//extraPlugins: 'wysiwygarea'
});
</script>


